package com.ruoyi.gis.componets;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.http.OkHttpUtils;
import com.ruoyi.framework.config.EarthAiConfig;
import com.ruoyi.gis.task.EarthAiTask;
import com.ruoyi.gis.task.RasterDataPublishLayerTask;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

@Component
public class RasterDatasetComponent {
    private final Logger logger = LoggerFactory.getLogger(RasterDatasetComponent.class);
    @Autowired
    EarthAiConfig earthAiConfig;
    @Autowired
    @Qualifier("rasterDataTaskExecutor")
    private ThreadPoolTaskExecutor taskExecutor;

    public void sendDownloadDataIdRequest(String dataId) {
        logger.info("sendDownloadDataIdRequest:.............."+dataId);
        JSONObject body = new JSONObject();
        body.put("dataId",dataId);
        OkHttpUtils.getInstance().init()
                .url(String.format("%s%s",earthAiConfig.getHost(),
                        earthAiConfig.getRasterDownload()))
                .jsonBodyPost(body)
                .execSync(new OkHttpUtils.OnResponse() {
                    @Override
                    public void onSuccess(Response response) {
                        logger.info("sendDownloadDataIdRequest success.......");
                    }

                    @Override
                    public void onError(Exception e) {
                        logger.info("sendDownloadDataIdRequest error .......");
                    }
                });
    }

    public void sendRasterDataPublishLayerMessage(Long dataRasterId) {
        taskExecutor.submit(new RasterDataPublishLayerTask(dataRasterId));
    }
}
